//
//  KDAutoMonitorController.m
//  WangJingApp
//
//  Created by 魏强 on 17/11/1.
//  Copyright © 2017年 KDTech. All rights reserved.
//

#import "KDAutoMonitorController.h"
#import "KDChildAutoMoController.h"
#import "KDTabBarController.h"
#import "WQDropdownMenuView.h"
#import "KDAutoRequestTool.h"
#import "KDAutoMonitorGroup.h"
#import "KDAMSubGroup.h"
#import "KDGetSubGroupListParam.h"

@interface KDAutoMonitorController ()
/**  <#Description#> */
@property (nonatomic, weak) WQDropdownMenuView *navMenuView;
/**  <#Description#> */
@property (nonatomic, strong) NSArray *groups;
/**
 *  对应group下的监测方案的集合
 */
@property (nonatomic, strong) NSArray * themeArr;
/**  <#Description#> */
@property (nonatomic, strong) KDAutoMonitorGroup * selectGroup;

@end

@implementation KDAutoMonitorController
#pragma mark - 懒加载区
- (NSArray *)themeArr
{
    if (!_themeArr) {
        _themeArr = [NSArray array] ;
    }
    return _themeArr;
}

- (NSArray *)groups
{
    if (!_groups) {
        _groups = [NSArray array] ;
    }
    return _groups;
}
#pragma mark - 生命周期区
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavBar];
    [self loadGroups];
    
    // Do any additional setup after loading the view.
}
#pragma mark - 板块布置区
- (void)setupNavBar{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"leftArrow"] style:UIBarButtonItemStylePlain target:self action:@selector(leftItemClick)];
    
}
/**
 *  设置中间的titleView
 */
- (void)setupNavMenuWith:(NSArray *)titles{
    WQDropdownMenuView *menuView = [[WQDropdownMenuView alloc] initWithFrame:CGRectMake(0, 0,100, 44) titles:titles];
    self.navMenuView = menuView;
    __weak __typeof(self)weakSelf = self;
    menuView.selectedAtIndex = ^(int index)
    {
        weakSelf.selectGroup = weakSelf.groups[index];
        [weakSelf loadData];
//        if (_projects.count) {
//            _currentIndex = index;
//            weakSelf.selectProject = _projects[index];
//            [weakSelf.tableView.header beginRefreshing];
//        }
    };
    menuView.width = 300;
    self.navigationItem.titleView = menuView;
}

- (void)setupAllChildViewController{
    if (self.childViewControllers.count) {
        for (UIViewController *child in self.childViewControllers) {
            //            KDLog(@"%@",child.title);
            [child removeFromParentViewController];
            [child.view removeFromSuperview];
        }
        //        self.roleValue = 0;
    }
    for (KDAMSubGroup *model in self.themeArr) {
        //        if ([model.isChecked  isEqual: @"1"]) {
        KDChildAutoMoController *child = [[KDChildAutoMoController alloc]init];
        child.title = model.title;
        //            child.view.backgroundColor = RandomColor;
        child.model = model;
        [self addChildViewController:child];
        //        }
    }
    // 4.设置所有标题
    self.needUnderLine = YES;
    [super setupAllTitle];
}
#pragma mark - 事件点击区
- (void)leftItemClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 网络请求区
/**
 *  请求所有的自主监测分组
 */
- (void)loadGroups{
    KDBaseParam *param = [[KDBaseParam alloc]init];
    param.method = @"earlywaring/getUserCanSeeEarlywaringGroups";
    WS(ws);
    [KDAutoRequestTool getAMGroupsWithParam:param success:^(KDBaseResult *responseObejct) {
        if ([responseObejct.code isEqualToString:@"1"]) {
            
            NSArray *groups = [KDAutoMonitorGroup objectArrayWithKeyValuesArray:responseObejct.info];
            ws.groups = groups;
            NSMutableArray *arr ;
            
            if (groups.count) {//如果有方案了
                arr = [NSMutableArray array];
                for (KDAutoMonitorGroup *project in groups) {
                    [arr addObject:project.group_name];
                }
                ws.selectGroup = ws.groups[0];
                
                //加载第一个group下的所有监测主题
                [ws loadData];
                
            }else{
                //创建没有方案时的添加方案界面
                //                [self setupNoProjectView];
                arr = [NSMutableArray arrayWithArray:@[@"请前往PC端添加监测"]];
            }
            [ws setupNavMenuWith:arr];

            
        }else if ([responseObejct.code isEqualToString:@"E040110000001"]){
            [self.navigationController dismissViewControllerAnimated:NO completion:^{
                KDNewfeatureController *new = [[KDNewfeatureController alloc]init];
                __block KDTabBarController*ctr = (KDTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                [UIApplication sharedApplication].keyWindow.rootViewController = new;
                [ctr removeObserver];
                [ctr removeFromParentViewController];
                ctr = nil;
            }];
        }
        else{
            [SVProgressHUD showErrorWithStatus:responseObejct.msg maskType:SVProgressHUDMaskTypeGradient];
        }
        
    } failure:^(NSError *error) {
        KDLog(@"error:\n%@",error);
        [SVProgressHUD showErrorWithStatus:@"网络异常" maskType:SVProgressHUDMaskTypeGradient];;
        
    }];
}
/**
 *  加载对应group下的所有监测主题
 */
- (void)loadData{
    KDGetSubGroupListParam *param = [[KDGetSubGroupListParam alloc]init];
    param.method = @"earlywaring/getUserCanSeeEarlywaringSettingList";
    param.typeId = [NSString stringWithFormat:@"%ld", self.selectGroup.type_id];
    param.groupId = [NSString stringWithFormat:@"%ld", self.selectGroup.group_id];
    
    [KDAutoRequestTool getMonitorListWithParam:param success:^(KDBaseResult *responseObejct) {
        if ([responseObejct.code isEqualToString:@"1"]) {
            NSArray *arr = responseObejct.data[@"eList"];
            self.themeArr = [KDAMSubGroup objectArrayWithKeyValuesArray:arr];
            [self setupAllChildViewController];
        }else{
            [SVProgressHUD showErrorWithStatus:responseObejct.msg maskType:SVProgressHUDMaskTypeGradient];
        }
        
    } failure:^(NSError *error) {
        KDLog(@"error:\n%@",error);
        [SVProgressHUD showErrorWithStatus:@"网络异常" maskType:SVProgressHUDMaskTypeGradient];;
        
    }];
}
#pragma mark - 通知接收区

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
